export class TrafficLights {
    constructor(lights) {
        this._lights = lights;
        this._currentIndex = 0;
        this._time = Date.now();
    }

    _update() {
        while (1) {
            if (this._disTime() <= this._lights[this._currentIndex].lasts) {
                break;
            }
            this._currentIndex = (this._currentIndex + 1) % this._lights.length;
            this._time = this.currentLight.lasts;
        }
    }

    get currentLight() {
        return this._lights[this._currentIndex];
    }

    _disTime() {
        return Date.now() - this._time;
    }

    getCurrentLight() {
        this._update();
        return {
            color: this.getCurrentLight().color,
            remain: this.getCurrentLight().lasts - this._disTime()
        }
    }
}